package com.dianrui.operation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.dianrui.operation.R;
import com.dianrui.operation.bean.Battery;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BatteryListAdapter extends BaseAdapter {
    Context context;
     List<Battery>list;
     BatteryClickListener batteryClickListener;

    public BatteryListAdapter(Context context, List<Battery> list) {
        this.context = context;
        this.list = list;
    }

    public void refresh(List<Battery> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface BatteryClickListener {
        void onBatteryClick(String msg_id);

    }

    public void setOnBatteryClickListener(BatteryClickListener messageClickListener) {
        this.batteryClickListener = messageClickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_battery_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (list.size()>0) {
            final Battery battery = list.get(position);
            holder.carNumbers.setText(battery.number);
            holder.battery.setText(battery.voltage+"%");
            holder.batteryBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    batteryClickListener.onBatteryClick(battery.number);

                }
            });
        }
        return convertView;
    }

     class ViewHolder {
        @BindView(R.id.car_numbers)
        TextView carNumbers;
        @BindView(R.id.battery)
        TextView battery;
        @BindView(R.id.battery_btn)
        Button batteryBtn;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
